package com.mp.ipm2020.admin.controller;

import com.mp.ipm2020.common.api.CommonResult;
import com.mp.ipm2020.mbg.model.ResourceCategory;
import com.mp.ipm2020.mbg.service.ResourceCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Auther adidasshe@mp.cn
 * <p>
 * 资源分类管理Controller
 */
@RestController
@Api(tags = "资源分类管理")
@RequestMapping("/resourceCategory")
public class ResourceCategoryController {
    @Autowired
    private ResourceCategoryService resourceCategoryService;

    @ApiOperation("查询所有后台资源分类")
    @GetMapping()
    public CommonResult<List<ResourceCategory>> listAll() {
        List<ResourceCategory> resourceList = resourceCategoryService.list();
        return CommonResult.success(resourceList);
    }

    @ApiOperation("添加后台资源分类")
    @PostMapping()
    public CommonResult create(@RequestBody ResourceCategory resourceCategory) {
            resourceCategory.setCreateTime(new Date());
        boolean count = resourceCategoryService.save(resourceCategory);
        if (count == true) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台资源分类")
    @PutMapping("/{id}")
    public CommonResult update(@PathVariable Long id,
                               @RequestBody ResourceCategory resourceCategory) {
        resourceCategory.setId(id);
        boolean count = resourceCategoryService.updateById(resourceCategory);
        if (count == true) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID删除后台资源")
    @DeleteMapping("/{id}")
    public CommonResult delete(@PathVariable Long id) {
        boolean count = resourceCategoryService.removeById(id);
        if (count == true) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
